package cn.neusoft.controller;

import cn.neusoft.domain.CargoDescription;
import cn.neusoft.domain.CargoDistributeBill;
import cn.neusoft.domain.ProviderInf;
import cn.neusoft.service.ProviderService;
import cn.neusoft.tool.PageSet;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;


@RestController
@RequestMapping("/provider")
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    @PostMapping("/register")
    @ApiOperation("供应商注册")
    public ResultBean providerRegister(@RequestBody ProviderInf providerInf) {
        ResultBean resultBean = null;

        if(providerInf != null){
            int reply = providerService.providerRegister(providerInf);
            switch(reply){
                case 1:
                    resultBean = ResultBean.success();
                    break;
                case 0:
                    resultBean = ResultBean.error(5001, "失败");
                    break;
                default:
                    resultBean = ResultBean.error(5002, "未知错误");
                    break;
            }
        }
        else{
            resultBean = ResultBean.error(5000, "输入为空");
        }

        return resultBean;
    }

    @ApiOperation("供应商删除")
    @GetMapping("/delete/{name}")
    public ResultBean providerDelete(@PathVariable("name") String name) {
        ResultBean resultBean = null;

        if(name != null){
            int reply = providerService.providerDelete(name);
            switch(reply){
                case 1:
                    resultBean = ResultBean.success();
                    break;
                case 0:
                    resultBean = ResultBean.error(5011, "失败");
                    break;
                default:
                    resultBean = ResultBean.error(5012, "未知错误");
                    break;
            }
        }
        else{
            resultBean = ResultBean.error(5010, "输入为空");
        }

        return resultBean;
    }

    @ApiOperation("查询供应商")
    @GetMapping("/query/{name}")
    public ResultBean providerQuery(@PathVariable("name") String name) {
        ResultBean resultBean = null;

        if(name != null){
            ProviderInf providerInf = providerService.providerQuery(name);
            if(providerInf != null){
                resultBean = ResultBean.success(providerInf);
            }
            else{
                resultBean = ResultBean.error(5021, "失败");
            }
        }
        else{
            resultBean = ResultBean.error(5020, "输入为空");
        }

        return resultBean;
    }

    @ApiOperation("查询所有供应商")
    @GetMapping("/queryAll")
    public ResultBean providerQueryAll() {
        ResultBean resultBean = null;

        resultBean = ResultBean.success(providerService.providerQueryAll());

        return resultBean;
    }

    @ApiOperation("更改供应商信息")
    @PostMapping("/update")
    public ResultBean providerChange(@RequestBody ProviderInf providerInf) {
        ResultBean resultBean = null;

        if(providerInf != null){

            ProviderInf find = providerService.getProviderByID(providerInf.getId());
            if(find != null){
                providerInf.copy(find);
                int reply = providerService.providerChange(find);
                switch(reply){
                    case 1:
                        resultBean = ResultBean.success();
                        break;
                    case 0:
                        resultBean = ResultBean.error(5041, "失败");
                        break;
                    default:
                        resultBean = ResultBean.error(5042, "未知错误");
                        break;
                }
            }

        }
        else{
            resultBean = ResultBean.error(5040, "输入为空");
        }

        return resultBean;
    }

    @PostMapping("/getAllProviderPage")
  public ResultBean getAllProviderPage(@RequestBody Map<String, String> map){
    ResultBean resultBean;
    List<ProviderInf> ans = null;
    if(map != null){
      String name = map.get("name");
      String pageNum = map.get("pageNum");
      String pageSize = map.get("pageSize");

      if(name.equals("")){
        ans = providerService.providerQueryAll();
      }
      else{
          ans = new ArrayList<>();
          ProviderInf providerInf = providerService.providerQuery(name);
          if(providerInf != null){
              ans.add(providerInf);
          }
      }
      PageSet<ProviderInf> pageSet = new PageSet<>(ans, Integer.parseInt(pageSize));
      Map<String, Object> reply = new HashMap<>();
      reply.put("total", pageSet.getTotal());
      reply.put("list", pageSet.getPage(Integer.parseInt(pageNum)));
      resultBean = ResultBean.success(reply);
    }
    else{
      resultBean = ResultBean.error(5050, "错误");
    }


    return resultBean;
  }

}
